#TidyTuesday

Import libraries and data

## -- Attaching packages --------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.1     v purrr   0.3.2
## v tibble  2.1.3     v dplyr   0.8.3
## v tidyr   0.8.3     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## -- Conflicts ------------------------------------------------------------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## loaded package tidyverse
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
## loaded package plotly
## here() starts at C:/Users/evan/Documents/TidyTuesday
## loaded package here
## Parsed with column specification:
## cols(
##   city = col_character(),
##   state = col_character(),
##   city_size = col_character(),
##   mode = col_character(),
##   n = col_double(),
##   percent = col_double(),
##   moe = col_double(),
##   state_abb = col_character(),
##   state_region = col_character()
## )
## file found. loading from local
## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`

## Warning: Factor `state_abb` contains implicit NA, consider using
## `forcats::fct_explicit_na`
#set geo
g <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = "#C0C0C0", bgcolor = "#38383a", domain=list(x=c(-1,2), y=c(-1,2)), framewidth=0
)

#init map
plot_geo(locationmode = 'USA-states') %>%

  #add individual traces for each subset (mode x city size)  
  add_trace(
    data = cloro %>% filter(city_size == "Small", mode == "Bike"),
    z = ~n,
    hoverinfo="none",
    locations = ~ state_abb,
    colorscale = c("Greens"),
    reversescale = T,
    visible = T,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Bikers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  add_trace(
    data = cloro %>% filter(city_size == "Medium", mode == "Bike"),
    z = ~n, 
    hoverinfo="none",
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Bikers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  add_trace(
    data = cloro %>% filter(city_size == "Large", mode == "Bike"),
    z = ~ n,
    hoverinfo="none",
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Bikers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  add_trace(
    data = cloro %>% filter(city_size == "Small", mode == "Walk"),
    z = ~ n,
    hoverinfo="none",
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Walkers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = "
    "
  ) %>%
  add_trace(
    data = cloro %>% filter(city_size == "Medium", mode == "Walk"),
    z = ~ n,
    hoverinfo="none",
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Walkers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  add_trace(
    data = cloro %>% filter(city_size == "Large", mode == "Walk"),
    z = ~ n,
    hoverinfo="none",
    locations = ~ state_abb,
    reversescale = T,
    visible = F,
    marker = list(line = list(
      color = toRGB("lightgrey"), width = 0.5
    )),
    colorbar = list(
      title = list(text = "Walkers",
                   font = list(
                     family="Overpass",
                     size=11,
                     color = "#008888"),
                     side="top"),
      bordercolor = "#008888",
      outlinecolor = "#008888",
      tickcolor = "#008888",
      tickfont = list(color = "#008888"),
      len = 0.75,
      y = .6, yanchor="center",
      thickness = 30
    ),
    name = ""
  ) %>%
  
  add_markers(data = locations %>% filter(city_size == "Small", mode == "Bike"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n,
              opacity=0.8, 
              hoverinfo = "text",
              text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " bikers / ", state.mode.tot, " ", state_abb, " bikers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " bikers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " small city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=T
  ) %>%
  add_markers(data = locations %>% filter(city_size == "Medium", mode == "Bike"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n,
              opacity=0.8, 
              hoverinfo = "text",
               text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " bikers / ", state.mode.tot, " ", state_abb, " bikers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " bikers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " medium city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=F
  ) %>%
  add_markers(data = locations %>% filter(city_size == "Large", mode == "Bike"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n,
              opacity=0.8, 
              hoverinfo = "text",
               text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " bikers / ", state.mode.tot, " ", state_abb, " bikers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " bikers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " large city bikers / ", state.mode.tot, " ", state_abb, " bikers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " bikers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"),  
              showlegend=F, visible=F
  ) %>%
  add_markers(data = locations %>% filter(city_size == "Small", mode == "Walk"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n,
              opacity=0.8, 
              hoverinfo = "text",
               text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " walkers / ", state.mode.tot, " ", state_abb, " walkers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " walkers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " small city walkers / ", state.mode.tot, " ", state_abb, " walkers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " walkers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=F
  ) %>% 
  add_markers(data = locations %>% filter(city_size == "Medium", mode == "Walk"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n,
              opacity=0.8, 
              hoverinfo = "text",
              text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " walkers / ", state.mode.tot, " ", state_abb, " walkers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " walkers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " medium city walkers / ", state.mode.tot, " ", state_abb, " walkers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " walkers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=F
  ) %>%
  add_markers(data = locations  %>% filter(city_size == "Large", mode == "Walk"),
              type="scattergeo",
              x = ~lon, 
              y = ~lat, 
              marker=list(color = "#FFDE03",
                          line=list(color="#FFDE03")),
              size=~n,
              opacity=0.8, 
              hoverinfo = "text",
              text = ~paste0(city, " ", state_abb, "<br />",
                            mode, ": ", n, " (", percent, "% ", "&plusmn;", moe, ")", "</br></br>",
                            
                            n, " ",city, " walkers / ", state.mode.tot, " ", state_abb, " walkers ",
                            "(", round(100*n/state.mode.tot,2), "%)</br>",
                            n, " ",city, " walkers / ", state.tot, " ", state_abb, " commuters ",
                            "(", round(100*n/state.tot,2), "%)","</br></br>",
                            
                            "-----Satewide-----</br>",
                            state.cat.tot, " ", state_abb, " large city walkers / ", state.mode.tot, " ", state_abb, " walkers  ",
                            "(", round(100*state.cat.tot/state.mode.tot,2), "%)</br>",
                            state.mode.tot, " ", state_abb, " walkers / ", state.tot, " ", state_abb, " commuters  ",
                            "(", round(100*state.mode.tot/state.tot,2), "%)"), 
              showlegend=F, visible=F
  ) %>%
  
  layout(title = list(text = '<br><br>Commuters by State',
                      font=list(color = "#008888", size = 20)),
         geo = g, 
         autosize=T,
         margin = list(l=0,r=0, b=0, t=0, pad=0.01),
         paper_bgcolor = "#38383a",
         updatemenus = list(
           list(
           type = 'buttons',
           direction = "right",
           xanchor="right",
           yanchor="bottom",
           x=0.499, y=0.075,
           bgcolor="lightgreen",
           font= list(
             size=9
           ),
           showactive=F,
           buttons = list(
             list(
               method = "update",
               args = list(list(
                 visible = c(T, F, F, F, F, F,T, F, F, F, F, F),
                 colorscale = c("Greens")
               )),
               label = "Small Cities - Bikers"
              ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, T, F, F, F, F,F, T, F, F, F, F),
                 colorscale = c("Greens")
               )),
               label = "Medium Cities - Bikers"
             ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, T, F, F, F,F, F, T, F, F, F),
                 colorscale = c("Greens")
               )),
               label = "Large Cities - Bikers"
             ))),
           list(
           type = 'buttons',
           direction = "right",
           xanchor="left",
           yanchor="bottom",
           x=.501, y=.075,
           bgcolor = "lightblue",
           font= list(
             size=9
           ),
           showactive=F,
           buttons = list(
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, F, T, F, F,F, F, F, T, F, F),
                 colorscale = c("Blues")
               )),
               label = "Small Cities - Walkers"
             ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, F, F, T, F,F, F, F, F, T, F),
                 colorscale = c("Blues")
               )),
               label = "Medium Cities - Walkers"
             ),
             list(
               method = "update",
               args = list(list(
                 visible = c(F, F, F, F, F, T,F, F, F, F, F, T),
                 colorscale = c("Blues")
               )),
               label = "Large Cities - Walkers"
             )
           )
         ))) %>%
  add_annotations(text = "Hover for breakdown - Use bottons to change city size and Commuter Mode",
                     xref = "paper", yref = "paper",
                     x = .05, xanchor = "left",
                     y = .05, yanchor = "top",
                     legendtitle = TRUE,
                     showarrow = FALSE,
                     font = list(color = '#008888',
                              size = 11))
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

## Warning: `line.width` does not currently support multiple values.

Representation of daily commuters by location surveyed. Buttons the map toggle by city size (small, medium or large) and commute mode (bikers or walkers). Each state is colored by total number of commuters of the selected mode and each yellow marker indicates individual cities surveye. Highlight individual cities to get more detailed information about

Finished!

###########################3 #leftovers

Evan W Barba

2019-11-05